<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Session Storage Testing</title>
  <style>
    button {
      margin-top: 5px;
      margin-left: 5px;
    }
    input {
      width: 300px;
    }
  </style>
</head>
<body onload="load()">
  <h3>Session Storage Testing</h3>
  <input name="test" placeholder="type something, then click the save button.">
  <br>
  <button onclick="saveData()">Save data</button>
  <button onclick="saveJson()">Save Json</button>
  <button onclick="removeData()">Remove data</button>
  <button onclick="clearData()">Clear data</button>
  <br>

  <script type="text/javascript">
    var input = document.getElementsByName('test')

    function load(){
      var lastValue = sessionStorage.getItem('my-session-test-data')
      if (input) {
        input[0].value = lastValue
        console.log('load data from local storage: ', lastValue)
      }
    }

    function saveData() {
      if (input) {
        sessionStorage.setItem('my-session-test-data', input[0].value)
        console.log('save data to local storage.')
      }else{
        alert('Cannot find the input contorl!')
      }
    }

    function saveJson() {
      if (input) {
        var person = {name: 'Vue3 learning', days: Math.ceil(Math.random() *100)}
        sessionStorage.setItem('person_session', JSON.stringify(person))
      }
    }

    function removeData() {
      sessionStorage.removeItem('my-session-test-data')
      if (!sessionStorage.getItem('my-test-data')){
        console.log('Data has been removed.')
      }
    }

    function clearData() {
      sessionStorage.clear()
      console.log('Data has been cleared.')
    }
  </script>
</body>
</html>